package com.aidong.stadium.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.stadium.biz.MstBrandBiz;
import com.aidong.stadium.biz.MstStoreBiz;
import com.aidong.stadium.entity.MstBrand;
import com.aidong.util.CountDistance;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.aidong.util.SortUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 品牌action
 * @author java
 * @version 1.0    
 *  2015.6.9
 */
@ParentPackage("json")
@Namespace("/")
public class MstBrandAction implements ModelDriven<MstBrand> {
	
	private MstBrand mstBrand; // 品牌实体类

	@Override
	public MstBrand getModel() {
		if(mstBrand == null){
			mstBrand = new MstBrand();
		}
		return mstBrand;
	}
	
	private MstBrandBiz mstBrandBiz; // 品牌service
	
	private MstStoreBiz mstStoreBiz; // 门店service

	/**
	 * 品牌service
	 * @param mstBrandBiz
	 */
	@Autowired
	public void setMstBrandBiz(MstBrandBiz mstBrandBiz) {
		this.mstBrandBiz = mstBrandBiz;
	}
	
	/**
	 * 门店service
	 * @param mstStoreBiz
	 */
	@Autowired
	public void setMstStoreBiz(MstStoreBiz mstStoreBiz) {
		this.mstStoreBiz = mstStoreBiz;
	}
	
	private int pageCurrent; // 当前页

	/**
	 * 当前页
	 * @param pageCurrent
	 */
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	private Map<String, Object> map; // 返回的json数据
	
	/**
	 * 返回的json数据
	 * @return
	 */
	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	/**
	 * 品牌推荐
	 * @return
	 */
	@Action(value="getmstbrand",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMstBrand(){
		map = new HashMap<String, Object>();
		try {
			List<Map<String, Object>> brandMap = mstBrandBiz.getMstBrand();
			for (Map<String, Object> brand : brandMap) {
				String brandLogoUrl = ReadPropertiesUtil.getIMGREAD(brand.get("brandLogo"));
				brand.remove("brandLogo");
				brand.put("brandLogo", brandLogoUrl);
			}
			map.put("code", 1);
			map.put("result", brandMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 品牌列表
	 * @return
	 */
	@Action(value="getmstbrandmore",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMstBrandMore(){
		map = new HashMap<String, Object>();
		try {
			String brandName = mstBrand.getBrandName(); // 品牌名称
			if(!"".equals(brandName) && brandName != null){
				brandName = "%" + brandName + "%";
			}
			List<Map<String, Object>> brandMap = mstBrandBiz.getMstBrandMore(brandName);
			for (Map<String, Object> brand : brandMap) {
				String brandLogoUrl = ReadPropertiesUtil.getIMGREAD(brand.get("brandLogo"));
				brand.remove("brandLogo");
				brand.put("brandLogo", brandLogoUrl);
			}
			map.put("code", 1);
			map.put("result", brandMap);
			return "succ";
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 品牌下的banner图片和场馆
	 * @return
	 */
	@Action(value="getbranddetail",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getBrandDetail(){
		map = new HashMap<String, Object>();
		try {
			int brandId = mstBrand.getBrandId(); // 品牌ID
			Map<String, Object> resultMap = new HashMap<String, Object>();
			if(brandId > 0){
				// 品牌banner图片
				List<Map<String, Object>> bannerMap = mstBrandBiz.getMstBrandBanner(brandId);
				for (Map<String, Object> banner : bannerMap) {
					String picUrl = ReadPropertiesUtil.getIMGREAD(banner.get("picUrl"));
					banner.remove("picUrl");
					banner.put("picUrl", picUrl);
				}
				// 分页
				Map<String, Object> pageMap = PageUtil.page(100, pageCurrent);
				pageMap.put("brandId", brandId); // 品牌ID
				// 门店列表
				List<Map<String, Object>> storeMap = mstStoreBiz.getMstStore(pageMap);
				
				// 记录条数
				int storeCount = mstStoreBiz.getMstStoreByIdCount(brandId);
				
				for (Map<String, Object> store : storeMap) {
					store.put("brandUrl", ReadPropertiesUtil.getIMGREAD(store.get("brandUrl")));
				}
				
				for (Map<String, Object> store : storeMap) {
					String coordinate = (String) store.get("coordinate"); // 场馆所在经纬度
					double longitude = mstBrand.getLongitude(); // 请求经度
					double latitude = mstBrand.getLatitude(); // 请求纬度
					if ((!"".equals(coordinate) && coordinate != null)
							&& longitude > 0 && latitude > 0){
						String[] location = coordinate.split(",");
						double discance = CountDistance.distance(
								longitude, latitude,
								Double.parseDouble(location[0]),
								Double.parseDouble(location[1]));
						int dis = (int) Math.rint(discance); // 四舍五入取整数
						store.put("count", dis);
						int distance = dis / 1000;
						if(discance < 1){
							store.put("distance", dis + " m"); // 相距距离....
						}else{
							store.put("distance", distance + " km"); // 相距距离....
						}
					}else{
						store.put("count", 0);
					}
					store.remove("coordinate");
				}
				resultMap.put("banner", bannerMap);
				// 按距离排序
				if(storeMap != null){
					SortUtil.distanceSort(storeMap);
				}
				resultMap.put("store", storeMap);
				map.put("code", 1);
				map.put("pageCurrent", pageMap.get("pageCurrent")); // 当前页
				map.put("count", storeCount);
				map.put("result", resultMap);
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}